package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-product-of-splitted-binary-tree/description/
 * @create 2025/9/13 15:59
 * @since JDK17
 */

public class Solution27 {
    long ans, sum;
    int N = ((int) (1e9 + 7));
    int f(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int l = f(root.left);
        int r = f(root.right);
        ans = Math.max(ans, Math.max((sum - l) * l, (sum - r) * r));
        return l + r + root.val;
    }
    int sum(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return root.val + sum(root.left) + sum(root.right);
    }
    public int maxProduct(TreeNode root) {
        sum = sum(root);
        f(root);
        return ((int) (ans % N));
    }
}
